<?php
include_once 'dbinteract.php';

class Logger {
  
  /* set $loggerinstance to file name if log has to be written to file or set to DB, whose configurations are picked from DB config file.*/
	private $loggerinstance="DB";
	private $userId;
	private $remoteIP;
	private $sourceserviceId;
	private $sourceloginID;
	private $targetServiceId;
	private $noofPhotoscopied;
	private $targetLoginID;
	private $starttime;
	private $extraInfo;
	
	/*Getters - Enable if we need to get the object state anytime.
		
		public function getremoteIP(){
		}	
		public function getsourceserviceId(){
		}
		public function getsourceloginID(){
		}
		public function gettargetServiceId(){
		}
		public function gettargetLoginID(){
		}
		public function getstarttime(){
		}
		public function getendtime(){
		}
		public function getaccessTime(){
		}
	*/

  /*Setters*/
	public function setremoteIP($remoteIPAddress){
		$this->remoteIP=$remoteIPAddress;
	}	
	public function setsourceserviceId($serviceId){
		$this->sourceserviceId=$serviceId;
	}
	public function setsourceLoginID($loginID){
		$this->sourceloginID=$loginID;
	}
	public function settargetServiceId($serviceId){
		$this->targetServiceId=$serviceId;
	}
	public function settargetLoginID($loginID){
		$this->targetLoginID=$loginID;
	}
	public function  setnoofPhotoscopied($photoscopied){
		$this->noofPhotoscopied=$photoscopied;
	}

	public function  setuserId($uId){
		$this->userId=$uId;
	}
	 //$msg length is limited to 100 chars in case of DB.
  	public function setextraInfo($msg) {
		$this->extraInfo=$msg;
  	}
  
  public function __construct($loggerinstance="DB",$remoteIP=null, $sourceserviceId=null, $sourceloginID=null, $targetServiceId=null, $targetLoginID=null) 
  {
	$this->starttime=time();
	$this->userId=1;
	$this->loggerinstance=$loggerinstance;
  }
	
 
  
	/*
	$srcdate ==> Specify start the date of records to be deleted.(Say 01/11/2011)
	$tgtdate ==> Specify end the date of records to be deleted.(Say 01/12/2011)
	$numdays ==> Specify start the date and number of days from the start date to be deleted.
	$allrec  ==> Set to true to purge all the records.
	*/
   public function purgeLog($srcdate=null,$tgtdate=null,$numdays=null,$allrec=false) {

  }
  
  public function write() {
		$this->timespent=time()-$this->starttime;
		
		if ( $this->loggerinstance == "DB"){
			$logQuery="insert into log values('".$this->userId."','".$this->remoteIP ."','". $this->sourceserviceId."','". $this->sourceloginID."','".$this->noofPhotoscopied."','".$this->targetServiceId."','".$this->targetLoginID."','".$this->timespent."','".$this->starttime."','".$this->extraInfo."');";
			echo $logQuery;
			//executeQuery($logQuery);
		}
		else {
			$fh = fopen( $this->loggerinstance, 'a+') or die("can't open file");
			$logMsg= $this->userId.','. $this->remoteIP .','.  $this->sourceserviceId.','.  $this->sourceloginID.','. $this->targetServiceId.','. $this->noofPhotoscopied.','. $this->targetLoginID.','. $this->timespent.','. $this->starttime.','. $this->extraInfo."\n";
			fwrite($fh, $logMsg);
			fclose($fh);
		}
		
		
  }
    
  
}  
?>